home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / blurb_20.zip / BLURB.CPP < prev    next >
C/C++ Source or Header  |  1992-04-28  |  18KB  |  615 lines

  1. //--------------------------------BLURB.CPP---------------------------------
  2. //
  3. //  Blurb v2.0  This is the updated version of the fortune program.
  4. //
  5. //--------------------------------------------------------------------------
  6. //
  7. //                                 Notice:
  8. //
  9. //                      Confidential and Proprietary
  10. //
  11. //  This work contains valuable confidential and proprietary information.
  12. //  Disclosure, use or reproduction without the written authorization from
  13. //  Jon Privatt is prohibited.  This unpublished work by Jon Privatt is
  14. //  protected by the laws of the United States and other countries.
  15. //
  16. //  If publication of the work should occur the following notice shall apply:
  17. //
  18. //  "Copyright (c) 1992, Jon Privatt, All Rights Reserved"
  19. //
  20. //--------------------------------------------------------------------------
  21.  
  22. #include <time.h>
  23. #include <stdio.h>
  24. #include <dos.h>
  25. #include <conio.h>
  26. #include <dir.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <io.h>
  30. #include <ctype.h>
  31.  
  32.  
  33. //===========================COMPILER DEFINITIONS===========================
  34.  
  35.  
  36. //===============================GLOBAL TYPES===============================
  37.  
  38.  
  39. //=============================GLOBAL VARIABLES=============================
  40. char  ch,
  41.       *p,
  42.       *q,
  43.       outlines[26][134],
  44.       blurb[1001];                // blurb input.
  45.  
  46. unsigned long
  47.       choice,
  48.       next=0,
  49.       num;                     // number of blurbs
  50.  
  51. unsigned seed=0;
  52.  
  53. time_t first, second;
  54. long curpos;
  55.  
  56. int   x=2,                     // current x position
  57.       y=1,                     // current y position
  58.       n=0,                     // position within blurb string!
  59.       delayvalue=50,
  60.       length,
  61.       aa=2,
  62.       nodelay=0,
  63.       lines,
  64.       words,                   // number of words in the blurb.
  65.       mxx,                     // maximum x position
  66.       mxy,                     // maximum y position
  67.       bcolor=9,                // background color
  68.       fcolor=15;               // foreground color
  69.  
  70. struct ftime
  71.       fnow,                    // time/date of last reindex
  72.       fthen;                   // time/date now
  73.  
  74. struct text_info
  75.       ti;
  76.  
  77. enum BOOLEAN {false, true}
  78.       quit=false,
  79.       end=false,
  80.       displayblurb=true,
  81.       mixup=false,
  82.       inorder=false,
  83.       autodelay=false,
  84.       border=true,
  85.       hide=false,               // hide the author line.  Alters aa=0
  86.       ERROR=false,              // error, Blurb renamed or altered if ==true.
  87.       codes=true;
  88.  
  89. FILE *blurbs,                   // file containing the blurbs
  90.      *cfg,                      // file containing configuration info.
  91.      *idx;                      // index file to blurbs
  92.  
  93.  
  94. //----------------------------------title------------------------------------
  95. void title() {
  96.   //Prints "Blurb v2.0"...encoded to prevent alteration.
  97.   cprintf("%c%c%c%c%c%c%c%c%c%c",66,108,117,114,98,32,118,50,46,48);
  98.  
  99.   gotoxy((mxy-18),1);
  100.   //Prints "Jon Privatt (c)1992"...encoded to prevent alternation.
  101.   cprintf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",74,111,110,32,80,114,105,118,97,116,116,32,40,99,41,49,57,57,50);
  102.   }
  103.  
  104. //--------------------------------get_cfg-----------------------------------
  105. int get_cfg() {
  106.  
  107.   p=searchpath("blurb.cfg");
  108.   if((cfg=fopen(p,"rb"))==NULL) return 1;
  109.  
  110.   fseek(cfg,39,SEEK_SET);              // position past file header.
  111.  
  112.   fread(&num, sizeof(unsigned long), 1, cfg);
  113.   fread(&border, sizeof(BOOLEAN), 1, cfg);
  114.   fread(&hide,sizeof(BOOLEAN),1,cfg);
  115.   fread(&bcolor, sizeof(int), 1, cfg);
  116.   fread(&fcolor, sizeof(int), 1, cfg);
  117.   fread(&delayvalue, sizeof(int), 1, cfg);
  118.   fread(&codes,sizeof(BOOLEAN),1,cfg);
  119.   fread(&autodelay,sizeof(BOOLEAN),1,cfg);
  120.   fread(&fthen, sizeof(fthen), 1, cfg);
  121.   fread(&next, sizeof(unsigned long), 1, cfg);
  122.   fread(&inorder,sizeof(BOOLEAN),1,cfg);
  123.   fread(&seed,sizeof(seed),1,cfg);
  124.   fclose(cfg);
  125.   return 0;
  126. }
  127.  
  128.  
  129. //-------------------------------save_cfg-----------------------------------
  130. void save_cfg() {
  131.   int yyyy;
  132.  
  133.   p=searchpath("blurb.cfg");
  134.   if (p==NULL) {                  // file not in path. Create new one
  135.     q=searchpath("blurb.exe");    //   wherever blurb.exe is located.
  136.     yyyy=strlen(q)-3;
  137.     q[yyyy++]='c';
  138.     q[yyyy++]='f';
  139.     q[yyyy]='g';
  140.       if ((cfg=fopen(q,"w+b"))==NULL) {
  141.       printf("Unable to create BLURB.CFG\n");
  142.       exit(1);
  143.       }
  144.     }
  145.     else {
  146.       if((cfg=fopen(p,"w+b"))==NULL) {
  147.     printf("Unable to create BLURB.CFG\n");
  148.     exit(1);
  149.     }
  150.       }
  151.   fputs("Blurb version 2.0\r\nConfiguration File\n",cfg); // File header.
  152.   fputc(26,cfg);                                          // EOF mark.
  153.   fwrite(&num, sizeof(unsigned long), 1, cfg);
  154.   fwrite(&border, sizeof(BOOLEAN), 1, cfg);
  155.   fwrite(&hide,sizeof(BOOLEAN),1,cfg);
  156.   fwrite(&bcolor, sizeof(int), 1, cfg);
  157.   fwrite(&fcolor, sizeof(int), 1, cfg);
  158.   fwrite(&delayvalue, sizeof(int), 1, cfg);
  159.   fwrite(&codes,sizeof(BOOLEAN),1,cfg);
  160.   fwrite(&autodelay,sizeof(BOOLEAN),1,cfg);
  161.   fwrite(&fthen, sizeof(fthen), 1, cfg);
  162.   fwrite(&next, sizeof(unsigned long),1,cfg);
  163.   fwrite(&inorder, sizeof(BOOLEAN), 1, cfg);
  164.   fwrite(&seed,sizeof(seed),1,cfg);
  165.   fclose(cfg);
  166. }
  167.  
  168. //------------------------------reindex-----------------------------------
  169. void reindex() {
  170.   long fl;                           // total length of the file.
  171.   long temp, percent, prev=-1;
  172.   int yyyy;
  173.  
  174.   num=1;
  175.   textcolor(7);
  176.   textbackground(0);
  177.   clrscr();
  178.   title();
  179.   gotoxy(1,3);
  180.   cprintf("%clurbs has been altered.  %cuto-%ceindex %cngaged...",66,65,82,69);
  181.   p=searchpath("blurbs");
  182.   if((blurbs=fopen(p,"rt"))==NULL) {
  183.     printf("Unable to find BLURBS\n");
  184.     exit(1);
  185.     }
  186.   p=searchpath("blurb.idx");
  187.   if (p==NULL) {                  // file not in path. Create new one
  188.     q=searchpath("blurb.exe");    //   wherever blurb.exe is located.
  189.     yyyy=strlen(q)-3;
  190.     q[yyyy++]='i';
  191.     q[yyyy++]='d';
  192.     q[yyyy]='x';
  193.     if ((idx=fopen(q,"w+b"))==NULL) {
  194.       printf("Unable to create BLURB.IDX\n");
  195.       fclose(blurbs);
  196.       exit(1);
  197.       }
  198.     }
  199.   else {
  200.     if((idx=fopen(p,"w+b"))==NULL) {
  201.       printf("Unable to create BLURB.IDX\n");
  202.       fclose(blurbs);
  203.       exit(1);
  204.       }
  205.     }
  206.   fseek(blurbs,0L,SEEK_END);
  207.   fl=ftell(blurbs);
  208.   gotoxy(1,4);
  209.   printf("%ceindexing:",82);
  210.   if (fl>100) temp=fl/100;
  211.   else percent=50;
  212.   rewind(blurbs);
  213.   curpos=ftell(blurbs);                 // write first record.
  214.   fwrite(&curpos,sizeof(long),1,idx);
  215.   while (fgets(blurb,1000,blurbs)!=NULL) {
  216.     num++;
  217.     curpos=ftell(blurbs);
  218.     if (fl>100) percent=curpos/temp;
  219.     if (percent!=prev) {
  220.       gotoxy(13,4);
  221.       prev=percent;
  222.       printf("%ld%%",percent);
  223.       }
  224.     fwrite(&curpos, sizeof(long), 1, idx);
  225.     }
  226.   gotoxy(13,4);
  227.   printf("100%%");
  228.   gotoxy(1,5);
  229.   printf("%lu BLURBS now indexed for maximum speed.\n",(num-1));
  230.   displayblurb=false;
  231.   fclose(idx);
  232.   getftime(fileno(blurbs),&fthen);
  233.   fclose(blurbs);
  234.   save_cfg();
  235. }
  236.  
  237.  
  238. //-------------------------------get_idx-----------------------------------
  239. void get_idx() {
  240.  
  241.   p=searchpath("blurbs");
  242.   if((blurbs=fopen(p,"rt"))==NULL) {  // check for updated BLURBS
  243.     printf("Unable to find BLURBS\n");
  244.     exit(1);
  245.     }
  246.   getftime(fileno(blurbs),&fnow);
  247.   fclose(blurbs);
  248.   if ((fnow.ft_tsec!=fthen.ft_tsec)||(fnow.ft_min!=fthen.ft_min)||(fnow.ft_hour!=fthen.ft_hour)||(fnow.ft_day!=fthen.ft_day)||(fnow.ft_month!=fthen.ft_month)||(fnow.ft_year!=fthen.ft_year))
  249.     reindex();
  250.  
  251.   p=searchpath("blurb.idx");
  252.   if((idx=fopen(p,"rb"))==NULL) reindex();
  253.   next++;
  254.   if (next>num) next=0;
  255.   if (inorder==true)
  256.     choice = next;
  257.   else
  258.     choice = (rand()%num);
  259.   curpos=(sizeof(long)*choice);
  260.   fseek(idx,curpos,SEEK_SET);
  261.   fread(&curpos,sizeof(long),1,idx);
  262.   fclose(idx);
  263.  
  264.   p=searchpath("blurbs");
  265.   if((blurbs=fopen(p,"rt"))==NULL) {
  266.     printf("Unable to find BLURBS\n");
  267.     exit(1);
  268.     }
  269.   fseek(blurbs,curpos,SEEK_SET);
  270.   fgets(blurb,1000,blurbs);
  271.   fclose(blurbs);
  272. }
  273.  
  274.  
  275. //-------------